

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dbmanager.DBManager;
import dbmanager.GetIdName;

/**
 * Servlet implementation class for Servlet: TransactionViewServlet
 *
 */
 public class TransactionViewServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public TransactionViewServlet() {
		super();
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		System.out.println("I'm in doGet()");
		GetIdName get = new GetIdName();
		HttpSession session = request.getSession(true);
        String objectselected=request.getParameter("objectname");    
        
        PrintWriter out   = response.getWriter();
			
		try 
		{
			out.write("<html><body>");
			out.flush();
			
			String param = request.getParameter("param");
				
			try
			{										
				if(param.equalsIgnoreCase("tt"))
				{
					ResultSet rsTran = DBManager.getSelect("select * from transaction where transaction_type='"+objectselected+"'");
					
					out.write("<table border='1' width='80%'>");
					while(rsTran.next())
					{
						String tempstring = rsTran.getString("ID");
						out.write("<tr><td><a href='TransactionViewServlet?objectname="+tempstring+"&param=id'>"+tempstring+"</a></td></tr>");
					}
					out.write("</table>");
				}
				else if(param.equalsIgnoreCase("id"))
				{
					String[] arrTran = {"Buyer","Seller","Order_date","Delivery_date","Transaction_type","Amount","Vat","SalesTax","ServiceTax","Octroi","Total_Amount"};
					String[] arrTranDetail = {"Item","Qty","Rate","Amount",};		
					
					//this.getTransactionReport("transaction", "trans_property_details", objectselected, arrTran, "id","tid");
					Hashtable<Object, Object> transaction_hash = new Hashtable<Object, Object>();
					
					try
			    	{
						// view transaction table
						int tempcounter = 0;
						
						transaction_hash.put("id", objectselected);
						
				    	ResultSet rsTran = DBManager.getSelect("select * from transaction where id='"+objectselected+"'");
				    						    	
				    	out.write("<table border='0' width='100%'>");
						out.write("<tr><td><font size=2>Transaction ID&nbsp;&nbsp;&nbsp;&nbsp;"+objectselected+"</font></td><td><font size=2><a href=UpdateServlet?id="+objectselected+">Edit</a></font></td><td><font size=2><a href=DeleteServlet?id="+objectselected+">Delete</a></font></td></tr>");
						out.write("</table><hr>");							
						out.write("<table border='0' width='100%'><tr>");
						
						while (rsTran.next())
						{
							for (int i = 0; i < arrTran.length; i++) 
							{						
								System.out.println(arrTran[i]);
								String tempstring = rsTran.getString(arrTran[i]);

								if (tempcounter == 2) 
								{
									out.write("</tr><tr>");
									tempcounter = 0;
								}

								
								
								if (arrTran[i].equalsIgnoreCase("seller")|| arrTran[i].equalsIgnoreCase("buyer")|| arrTran[i].equalsIgnoreCase("transaction_type"))
								{
									transaction_hash.put(arrTran[i], get.getItem(tempstring));
									tempstring = "<a href=TransactionViewServlet?objectname="+ tempstring+ ">"+ get.getItem(tempstring)+ "</a>";
								}
								else
								{
									transaction_hash.put(arrTran[i], tempstring);
								}
								
								out.write("<td><font size=2>" + arrTran[i]+ "</font></td><td><font size=2>"+ tempstring + "</font></td");

								tempcounter++;
							}
						}
						session.setAttribute("transaction", transaction_hash);
						
						
						out.write("</tr>");							
						out.flush();
						
						// view trans_property_details table
						ResultSet rstpl = DBManager.getSelect("select pid,pv from trans_property_details  where tid='"+objectselected+"'");
						
						while (rstpl.next())
						{
							out.write("<tr>");
							
							if(tempcounter==2)
							{
								out.write("</tr><tr>");
								tempcounter = 0;
							}
							
							String _pid = get.getItem(rstpl.getString("PID"));
							String _pv = get.getItem(rstpl.getString("PV"));
							
							out.write("<td><font size=2>"+_pid+"</font></td><td><font size=2>"+_pv+"</font></td");
							tempcounter++;		
						}
						
						out.write("</tr></table>");
						out.flush();
			    	}
			    	catch (Exception e) 
			    	{
						e.printStackTrace();
					}				    	
			    	
					//this.getTransactionReport("transaction_details", "td_property_details", objectselected, arrTranDetail,"tid","tdid");
			    	
			    	try
			    	{
						// view transaction_details table
				    	ResultSet rsTran = DBManager.getSelect("select * from transaction_details where tid='"+objectselected+"'");
				    	StringBuffer sbr1 = new StringBuffer();
				    	StringBuffer sbr2 = new StringBuffer();
				    	
				    	Vector<Object> transa_details = new Vector<Object>();
				    	
				    	out.write("<p></p><table border='1' width='80%'>");
				    						    
				    	StringBuffer tr_details =  new StringBuffer();
				    	
						while(rsTran.next())
						{
							for(int i=0;i<arrTranDetail.length;i++)
							{
								try
								{
									String itemcode = rsTran.getString(arrTranDetail[i]);
									
									if(arrTranDetail[i].equalsIgnoreCase("item"))
									{
										// Fetch item name using itemcode
										ResultSet _rs = DBManager.getSelect("select MID from property_details where pid='"+get.getId("itemcode")+"' and  pv='"+itemcode+"'");
										while(_rs.next())
										{
											String _item_id = _rs.getString("MID");
											sbr2.append("<td><font size=2><a href=TransactionViewServlet?objectname="+_item_id+">"+get.getItem(_item_id)+"</a></font></td>");
											tr_details.append(get.getItem(_item_id)+":");
										}
									}
									else if(arrTranDetail[i].equalsIgnoreCase("qty"))
									{
										sbr2.append("<td><font size=2>"+itemcode+"</font></td>");
										tr_details.append(itemcode+"\t");
									}
									else
									{
										sbr2.append("<td><font size=2>"+itemcode+"</font></td>");
									}
								}
								catch (Exception e)
								{
									e.printStackTrace();
									sbr2.append("<td><font size =2>-</font></td>");
									if(arrTranDetail[i].equalsIgnoreCase("item") || arrTranDetail[i].equalsIgnoreCase("qty"))
										tr_details.append(":-\t");
								}
								
							}
							
							String temp_buf = tr_details.toString();
							tr_details = new StringBuffer(temp_buf.substring(0, temp_buf.length()-1)+"\t");
							System.out.println(tr_details.toString());
							
							transa_details.add(sbr2.toString());
							sbr2 = new StringBuffer();
						}
						
						session.setAttribute("transaction_details", tr_details);
						
						// view td_property_details table
						ResultSet rstpl = DBManager.getSelect("select pid,pv from td_property_details  where tdid='"+objectselected+"'");
						
						while (rstpl.next())
						{
							String temp = get.getItem(rstpl.getString("PID"));
							sbr1.append("<td>"+temp+"</td>");
							String [] temparr = {temp};
							System.arraycopy(temparr, 0, arrTranDetail, arrTranDetail.length-1, 1);
															
							sbr2.append("<td>"+get.getItem(rstpl.getString("PV"))+"</td>");
						}
						
						for(int i=0;i<arrTranDetail.length;i++)
				    	{
				    		System.out.println("<td><font size=2>"+arrTranDetail[i]+"</font></td>");
							sbr1.append("<td><font size=2>"+arrTranDetail[i]+"</font></td>");								
				    	}
						
						out.write("<tr>"+sbr1.toString()+"</tr>");
						for(int i=0; i<transa_details.size();i++)
						{
							out.write("<tr>"+transa_details.get(i)+"</tr>");
						}
						out.flush();								
			    	}
			    	catch (Exception e) 
			    	{
						e.printStackTrace();
					}
				}
			}
			catch (NullPointerException e) 
			{
				e.printStackTrace();
			}
			
			out.write("</body></html>");
			out.flush();
				
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			out = null;
			get = null;
		}
		
	}   	  	    
}